<script type="text/javascript" src="../scripts/date.js"></script>
<script type="text/javascript">
	function guiform() 
	{  
		 document.formgui.submit(); 
	}
	
	function ChuyenMaKhachHang(MaKH){
		document.getElementById('MaKH').value = MaKH;
	}

	function GetThongTinKhachHang(MaKH){
		var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            var thongTin = xmlHttp.responseText;
	            var mangThongTin = thongTin.split("|");
	            document.getElementById('NgaySinh').value = mangThongTin[0];
	            document.getElementById('DiaChi').value = mangThongTin[1];
	            document.getElementById('SoCMND').value = mangThongTin[2];
	            document.getElementById('NgayCap').value = mangThongTin[3];
	            document.getElementById('NoiCap').value = mangThongTin[4];
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/taisanthechap/getthongtinkhachhang.php?makh='+MaKH, true);
	    xmlHttp.send(null);
	}

	function GetHinhThucChoVay(MaHinhThucVay){
		var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            var laiSuatVay = xmlHttp.responseText;
	            document.getElementById('LaiSuatChoVay').value = laiSuatVay;
	            if(laiSuatVay!="")
	            {
		            var laiSuatQuaHan = parseFloat(laiSuatVay) * 150 / 100
	            	document.getElementById('LaiSuatQuaHan').value = Math.round(laiSuatQuaHan*100)/100;
	            }
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/hopdongtindung/gethinhthucvay.php?mahinhthucvay='+MaHinhThucVay, true);
	    xmlHttp.send(null);
	}
	
	function tinhGiaTriDat(){
	 	var xmlHttp;
	 	var DienTichDat = document.getElementById('DienTichDat').value;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	        	document.getElementById('GiaTriTaiSan').value = Math.round(parseFloat(DienTichDat) * 50000000 / 1000);
	        }
	    }
	    xmlHttp.open('GET', '', true);
	    xmlHttp.send(null);
	}

	function ajaxFunction(id){
	    var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            document.getElementById('MaHinhThucVay').innerHTML = xmlHttp.responseText;
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/hopdongtindung/comboboxkyhantra.php?dmid='+id, true);
	    xmlHttp.send(null);
	}

	function ThietLapThangVay(SoThang) {
		var currentDate = new Date();
	  	var day = currentDate.getDate();
	  	var month = currentDate.getMonth() + 1;
	  	var year = currentDate.getFullYear();
		document.getElementById('NgayMoSo').value = year + "-" + month + "-" + day;
		var ngayToiHan = Date.today().addMonths(Number(SoThang)); 
		document.getElementById('NgayDenHan').value = ngayToiHan.getFullYear()+"-"+(ngayToiHan.getMonth()+1)+"-"+ngayToiHan.getDate();
	}
</script>
<?php 
	$tstc = new taisanthechapbs();
	$tstc->SoHDTC = $_GET["id"];
	$dstaisanthechap = $tstc->LayDanhSach();
?>
<h2><strong>Hợp đồng tín dụng</strong></h2>
<br/>
<form enctype="multipart/form-data" name="formgui"  method="post" action="index.php?do=laphopdongtindung_xuly" onsubmit="return checkPostArtForm(this);">
<table style="width:950px; margin-left: 10px; table-layout: fixed;">
		<tr>
			<td style="width:20px;padding-top:10px;">
	        	<label for="mataisanthechap">Số khế ước:</label>        	
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input readonly="readonly" tabindex="1" type="text" id="SoKheUoc" name="SoKheUoc" class="txt" size="12" value="<?php include 'getsokheuoc.php';?>"/>  
	        </td>
	        <td style="width:20px;padding-top:10px;">

	        </td>
	        <td style="width:60px;padding-top:10px;">

	        </td>
		</tr>
		<tr>
			<?php 
				
				$kh = new khachhangbs();
				$kh->MaKH = $dstaisanthechap[0]["MaKH"];
				$dskhachhang = $kh->LayDanhSach();
			?>
			<td style="padding-top:10px;">
	        	<label for="tenkhachhang">Khách hàng:</label>
	        </td>	       
			<td style="padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="TenKhachHang" name="TenKhachHang" class="txt" size="12" value="<?php echo $dskhachhang[0]['Ho'] . " ". $dskhachhang[0]['Ten'];?>" onchange="ChuyenMaKhachHang(this.value);GetThongTinKhachHang(this.value);"/>
			</td>
		</tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="makhachhang">Tài khoản:</label>      	
	        </td>
			 <td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="MaKH" name="MaKH" class="txt" size="12" value="<?php echo $dskhachhang[0]['MaKH'];?>" />
	        </td>
	        <td style="padding-top:10px;">
	        	<label for="ngaysinh">Ngày sinh:</label>            	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="NgaySinh" name="NgaySinh" class="txt" size="12" value="<?php echo $dskhachhang[0]["NgaySinh"];?>" /> 
	        </td>
		</tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="diachi">Địa chỉ:</label>         	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="DiaChi" name="DiaChi" class="txt" size="12" value="<?php echo $dskhachhang[0]["DiaChi"];?>" /> 
	        </td>
			<td style="padding-top:10px;">
	        	<label for="socmnd">Số CMND:</label>        	
	        </td>
			 <td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="SoCMND" name="SoCMND" class="txt" size="12" value="<?php echo $dskhachhang[0]["SoCMND"];?>" /> 
	        </td>
		</tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="ngaycap">Ngày cấp:</label>          	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="NgayCap" name="NgayCap" class="txt" size="12" value="<?php echo $dskhachhang[0]["NgayCap"];?>" /> 
	        </td>
			<td style="padding-top:10px;">
	        	<label for="noicap">Nơi cấp:</label>        	
	        </td>
			 <td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="NoiCap" name="NoiCap" class="txt" size="12" value="<?php echo $dskhachhang[0]["NoiCap"];?>" /> 
	        </td>
		</tr>		
		<tr>
			<td style="padding-top:10px;">
	        	<label for="nguoithuake">Người thừa kế:</label>        	
	        </td>	       
			 <td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="NguoiThuaKe" name="NguoiThuaKe" class="txt" size="12" value="<?php echo $dstaisanthechap[0]["NguoiThuaKe"];?>" /> 
	        </td>
	        <td style="padding-top:10px;">
	        	<label for="quanhe">Quan hệ:</label>&nbsp<span class="requirefield">*</span>         	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input placeholder ="Nhập mối quan hệ" tabindex="1" type="text" name="QuanHe" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa nhập mối quan hệ')" onchange="this.setCustomValidity('')"/>
	        </td>
		</tr>		
		<tr>
			<td colspan="0">
			<hr>
			</td>
		</tr>
		<tr><td style="padding-top:10px;">
	        	<label for="tstc" style="color: blue;"><strong>- TÀI SẢN THẾ CHẤP -</strong></label>         	
	        </td>
	    </tr>
	    <tr>
			<td style="padding-top:10px;">
	        	<label for="sohdtc">Số HĐTC:</label>         	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="SoHDTC" name="SoHDTC" class="txt" size="12" value="<?php echo $_GET["id"];?>" /> 
	        </td>
			<td style="padding-top:10px;">
	        	<label for="soqsdd">Số QSDĐ:</label>         	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="SoQSDD" name="SoQSDD" class="txt" size="12" value="<?php echo $dstaisanthechap[0]["SoQSDD"];?>" /> 
	        </td>
	    </tr>		
		<tr>
	        <td style="padding-top:10px;">
	        	<label for="tongdientich">Tổng diện tích:</label>        	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="TongDienTich" name="TongDienTich" class="txt" size="12" value="<?php echo $dstaisanthechap[0]["TongDienTich"];?>" />m2 
	        </td>
			<td style="padding-top:10px;">
	        	<label for="giatritaisan">Giá trị tài sản:</label>       	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="GiaTriTaiSan" name="GiaTriTaiSan" class="txt" size="12" value="<?php echo $dstaisanthechap[0]["GiaTriTaiSan"];?>" />đ
	        </td>
		</tr>
		<tr>
			<td colspan="0">
			<hr>
			</td>
		</tr>
		<tr><td style="padding-top:10px;">
	        	<label for="vv" style="color: blue;"><strong>- VAY VỐN -</strong></label>         	
	        </td>
	    </tr>
	    <tr>
			<td style="padding-top:10px;">
	        	<label for="mucchovay">Mức cho vay:</label>       	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input id="MucChoVay" readonly="readonly" tabindex="1" type="text" name="MucChoVay" class="txt" size="12" value="<?php echo $dstaisanthechap[0]["MucChoVay"];?>" />đ
	        </td>
		</tr>
	    <tr>
			<td style="padding-top:10px;">
	        	<label for="sotienchovay">Số tiền cho vay:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
			<td style="padding-top:10px;">
				<input placeholder ="Nhập số tiền cho vay" tabindex="1" type="text" name="SoTienChoVay" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa nhập số tiền cho vay')" onchange="this.setCustomValidity('')"/>		
			</td>
			<td style="padding-top:10px;">
	        	<label for="mucdichvay">Mục đích cho vay:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
			<td style="padding-top:10px;">
				<input placeholder ="Nhập mục đích vay" tabindex="1" type="text" name="MucDichVay" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa nhập mục đích vay')" onchange="this.setCustomValidity('')"/>					
			</td>
		</tr>
		<tr>
			<td style="padding-top:15px;">
	        	<label for="mahtvay">Hình thức cho vay:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
			<td style="padding-top:15px;">
				<select name="MaHinhThucVay" onchange="GetHinhThucChoVay(this.value);ajaxFunction(this.value);">	
            		<option value="0">-- Chọn hình thức vay --</option>	                      	
					<?php
						$hinhthucvay = new hinhthucvaybs();
						$danhsachhinhthucvay = $hinhthucvay->LayDanhSach();
						
						for($j = 0; $j < ($danhsachhinhthucvay != false ? count($danhsachhinhthucvay) : 0); $j++)
						{							
							echo "<option value='{$danhsachhinhthucvay[$j]["MaHinhThucVay"]}'>{$danhsachhinhthucvay[$j]['TenHinhThucVay']}</option>";
						}
					?>
				</select>			
			</td>
			<td style="padding-top:15px;">
	        	<label for="thoihanvay">Số tháng cho vay:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
			<td style="padding-top:15px;">
				<input type="number" id="SoThangVay" name="SoThangVay" value="0" onchange="ThietLapThangVay(this.value);"/> tháng
			</td>
		</tr>
		<tr>
			<td style="padding-top:15px;">
	        	<label for="ngaylaphopdong">Ngày lập hợp đồng:</label>
	        </td>
			<td style="padding-top:15px;">
				<input readonly="readonly" tabindex="1" type="text" id="NgayMoSo" name="NgayMoSo" class="txt" size="12" value="0" />
			</td>
			<td style="padding-top:15px;">
	        	<label for="ngaydenhan">Ngày đến hạn:</label>
	        </td>
			<td style="padding-top:15px;">
				<input readonly="readonly" tabindex="1" type="text" id="NgayDenHan" name="NgayDenHan" class="txt" size="12" value="0" />
			</td>
		</tr>
		<tr>
			<td style="padding-top:15px;">
	        	<label for="hinhthucxuly">Hình thức xử lý <br/>&nbsp &nbsp &nbsp &nbsp tài sản:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
	        <td style="padding-top:15px;">
	        	<input placeholder ="Nhập hình thức xử lý tài sản" tabindex="1" type="text" name="HinhThucXuLy" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa nhập hình thức xử lý tài sản')" onchange="this.setCustomValidity('')"/>
	    	</td>
			<td style="padding-top:15px;">
	        	<label for="laisuatchovay">Lãi suất cho vay:</label> 
	        </td>
	        <td style="padding-top:15px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="LaiSuatChoVay" name="LaiSuatChoVay" class="txt" size="54"/>
	    	</td>
	    </tr>
	    <tr>
	    	<td style="padding-top:15px;">
	        	<label for="kyhantra">Kỳ hạn trả nợ:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
			<td style="padding-top:15px;">
				<span id="MaHinhThucVay">
					<select name="MaKyHanTra" id="" onchange="this.setCustomValidity('')">	
	            		<option value="0">-- Chọn kỳ hạn trả nợ --</option>	                      	
						
					</select>
				</span>			
			</td>
			<td style="padding-top:15px;">
	        	<label for="laisuatquahan">Lãi suất quá hạn:</label> 
	        </td>
	        <td style="padding-top:15px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="LaiSuatQuaHan" name="LaiSuatQuaHan" class="txt" size="54" />
	    	</td>			
		</tr>
		<tr>
			<td colspan="0">
			<hr>
			</td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="sobuttoan">Số bút toán:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input readonly="readonly" tabindex="1" type="text" name="SoButToan" class="txt" size="54" value="<?php include '../adm/nhanvien/sotietkiem/getsobuttoan.php';?>"/>   
	        </td>
	        <td style="width:20px;padding-top:10px;">

	        </td>
	        <td style="width:60px;padding-top:10px;">

	        </td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="nhanvienlapso">Nhân viên lập sổ:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	        	<?php 
	        		$nguoidung = new nguoidungbs();
	        		$nguoidung->MaNguoiDung = $_SESSION["MaND"];
	        		$danhsach = $nguoidung->LayDanhSach();
	        		$HoTen = $danhsach[0]["Ho"]." ".$danhsach[0]["Ten"];
	        	?>
	        	<input type="hidden" name="MaNhanVien" value="<?php echo $danhsach[0]['MaNguoiDung'] ?>"/>
	            <input readonly="readonly" tabindex="1" type="text" name="TenNhanVien" class="txt" size="54" value="<?php echo $HoTen; ?>"/>   
	        </td>
	        <td style="width:15px;padding-top:10px;">
	        	<label for="loaigiaodich">Loại giao dịch:</label>&nbsp<span class="requirefield">*</span>   
	        </td>
	        <td style="width:60px;padding-top:10px;">
	        	<select name="MaLoaiGiaoDich">	
            		<option value="0">-- Chọn loại giao dịch --</option>	                      	
					<?php
						$loaigiaodich = new loaigiaodichbs();
						$dsloaigiaodich= $loaigiaodich->LayDanhSach();
						for($j = 0; $j < ($dsloaigiaodich != false ? count($dsloaigiaodich) : 0); $j++)
						{		
							echo "<option value='{$dsloaigiaodich[$j]["MaLoaiGiaoDich"]}'>{$dsloaigiaodich[$j]["TenLoaiGiaoDich"]}</option>";
						}
					?>
				</select>
	        </td>
		</tr>
		<p class="requirefields">(*): Các trường bắt buộc phải có.</p>
</table>
<br/>
<br/>
<p align="center">
	<input type="submit" class="button small blue" value="In phiếu chi tiền"/>
	&nbsp &nbsp &nbsp
	<a href="javascript:document.forms['formgui'].reset();" class="button small blue">Nhập lại</a>	
</p>
</form>
<br/>
<br/>
<?php
	if(isset($_SESSION["LoiThemHopDongTinDung"]))
	{
		echo $_SESSION["LoiThemHopDongTinDung"];
		unset($_SESSION["LoiThemHopDongTinDung"]);
	}
?>

<script type="text/javascript">
function checkPostArtForm(frm)
{
	
	var numberRe = /^[0-9]+$/;
	if(!frm.SoTienChoVay.value.match(numberRe))
	{	
		alert('Số tiền vay phải là số.');
		frm.SoTienChoVay.focus();
		return false;
	}

	if(frm.SoTienChoVay.value < 10000000)
	{
		alert('Số tiền cho vay phải >= 10 triệu!');
		frm.SoTienChoVay.focus();
		return false;
	}
	
	if(parseFloat(frm.SoTienChoVay.value) > parseFloat(frm.MucChoVay.value))
	{
		alert('Số tiền cho vay phải <= mức cho vay!');
		frm.SoTienChoVay.focus();
		return false;
	}
	
	if(frm.MaHinhThucVay.value==0)
	{
		alert('Bạn chưa chọn hình thức vay');
		frm.MaHinhThucVay.focus();
		return false;
	}
	
	if(frm.SoThangVay.value <= 0)
	{
		alert('Số tháng vay phải lớn hơn 0');
		frm.SoThangVay.focus();
		return false;
	}
	
	if(frm.MaKyHanTra.value==0)
	{
		alert('Bạn chưa chọn kỳ hạn trả');
		frm.MaKyHanTra.focus();
		return false;
	}
	
	if(frm.MaLoaiGiaoDich.value==0)
	{
		alert('Bạn chưa chọn loại giao dịch');
		frm.MaLoaiGiaoDich.focus();
		return false;
	}
	
	 var returnValue = confirm("Bạn chắc là thông tin hợp đồng tín dụng đúng?");
	    
	    if(returnValue == true)
	    {
	        alert("Tiến hành in phiếu chi tiền!");
	    }
	    else
	    {
	        return false;
	    }
    
    return true;
}
</script>

